//
//  AYMLeftController.m
//  QuickCure
//
//   Mr.LV
//
#import "AYMLeftController.h"
#import "AYMLoginViewController.h"
#import "AYMUserHeaderView.h"
#import "AYMHeadLoginView.h"
#import "AYMRegisterViewController.h"
#import "AYMFocusDoctorController.h"
#import "AYMCaseManagementController.h"
#import "AYMSettingController.h"
#import "AYMAboutProductController.h"
#import "AYMFamousDoctorController.h"
#import "AYMPersonalMSGController.h"

#import "UMSocial.h"


@interface AYMLeftController ()<UITableViewDataSource,UITableViewDelegate,AYMUserHeaderViewDelegate>

@property (nonatomic,strong) NSArray *titleNameArr1;
@property (nonatomic,strong) NSArray *titleNameArr2;
@property (nonatomic,weak) AYMHeadLoginView *headView2;
@property (nonatomic,weak) AYMUserHeaderView *headView;
@property (nonatomic,weak) UITableView *tableView;

@end
static NSString *identifier = @"leftCell";
@implementation AYMLeftController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleNameArr1 = @[@"名医通申请",@"病例管理",@"关注医生"];
    self.titleNameArr2 = @[@"设置与帮助",@"关于产品",@"分享给好友"];
    
    [self setupUI];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userEnterByaccount) name:@"UserLoginNoti" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userEnterByMessage) name:@"UserLoginByMessageNoti" object:nil];
    
}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [self setupHidden];
}

#pragma mark - 设置侧滑界面显示与隐藏
-(void)setupHidden{
    
    BOOL isLogin = [UserDefault boolForKey:@"isUserLogiKey"];
    
    self.headView2.hidden = isLogin;
    self.headView.hidden = !isLogin;
    
    if (isLogin) {
        
        [[UserData sharedInstance]loadUserInfoFromSanBox];

        [self.headView updateView];

    }
    [self.tableView reloadData];
}
#pragma mark - 布局界面
-(void)setupUI{
    
    self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    //顶部视图(未登录)
    AYMHeadLoginView *headView2 = [[AYMHeadLoginView alloc]init];
    self.headView2 = headView2;
    [self.view addSubview:headView2];
    
    //设置顶部视图(登录)
    AYMUserHeaderView *headView = [[AYMUserHeaderView alloc]init];
    self.headView = headView;
    [self.view addSubview:headView];
    headView.delegate = self;
    
    
    //登录后的侧滑tableView
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
    
    [headView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.top.trailing.equalTo(self.view);
        make.height.equalTo(120);
    }];
    [headView2 makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.top.trailing.equalTo(self.view);
        make.height.equalTo(120);
    }];
    [tableView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.equalTo(self.view);
        make.trailing.equalTo(self.view).offset(-70);
        make.top.equalTo(headView.bottom);
    }];
    
    
    
}

#pragma mark - 点击跳转至个人信息控制器代理方法实现
-(void)userHeaderView:(AYMUserHeaderView *)userHeaderView{

    AYMPersonalMSGController *personalVC = [[AYMPersonalMSGController alloc]init];
    [self jumpToTargetVC:personalVC];
}

#pragma mark - tableview数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    BOOL isLogin = [UserDefault boolForKey:@"isUserLogiKey"];
    if (!isLogin) {
        return 0;
    }else{
    
        return 2;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.selectionStyle = NO;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section == 0) {
        
        cell.textLabel.text = self.titleNameArr1[indexPath.row];
    }else{
        cell.textLabel.text = self.titleNameArr2[indexPath.row];
    }
    
    return cell;
}
#pragma mark - tableView代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
    
        if (indexPath.row == 0) {
            //名医通申请
            AYMFamousDoctorController *famousDocVC = [[AYMFamousDoctorController alloc]init];
            [self jumpToTargetVC:famousDocVC];
            
        }else if (indexPath.row == 1){
            //病例管理
            AYMCaseManagementController *caseMangeMentController = [[AYMCaseManagementController alloc] init];
            
            [self jumpToTargetVC:caseMangeMentController];
        
        }else{
        
            //关注医生
            AYMFocusDoctorController *focusDocVC = [[AYMFocusDoctorController alloc]init];
            [self jumpToTargetVC:focusDocVC];        

        }
    }else if (indexPath.section == 1){
    
        if (indexPath.row == 0) {
            
            //设置与帮助
            AYMSettingController *settingVC = [[AYMSettingController alloc]init];
            
            [self jumpToTargetVC:settingVC];
            WEAKSELF
            settingVC.logoutBlock = ^{
            
                [weakSelf setupHidden];
            };
            
        }else if (indexPath.row == 1){
        
            //关于产品
            AYMAboutProductController *aboutVC = [[AYMAboutProductController alloc]init];
            [self jumpToTargetVC:aboutVC];
        }else{
        
            //分享
            
            [UMSocialSnsService presentSnsIconSheetView:self appKey:@"572e217f67e58e4a47003a7d" shareText:@"分享好运" shareImage:nil shareToSnsNames:@[UMShareToSina,UMShareToTencent,UMShareToRenren] delegate:nil];
        }
    }
}


#pragma mark - 跳转控制器方法
-(void)jumpToTargetVC:(UIViewController *)targetVC{


    [self.sidePanelController showCenterPanelAnimated:YES];
    
    [(UINavigationController *)self.sidePanelController.centerPanel pushViewController:targetVC animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.1;
    }else{
        return 10;
    }
}

- (void)userEnterByaccount {
    
    //创建Login故事板
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    //创建Login控制器
    AYMLoginViewController *loginVC = [board instantiateViewControllerWithIdentifier:@"AYMLogin"];
    
    //回调登录界面block
    WEAKSELF
    loginVC.loginBlock = ^{
        [[UserData sharedInstance] loadUserInfoFromSanBox];
         [weakSelf setupHidden];
       
    };
    // 设置push出来的界面处在中间位置
    [self.sidePanelController showCenterPanelAnimated:YES];
    
    // 取到中间的nav push出登录界面
    [(UINavigationController *)self.sidePanelController.centerPanel pushViewController:loginVC animated:YES];
    
}


- (void)userEnterByMessage {
    AYMRegisterViewController *registerController = [[AYMRegisterViewController alloc]init];
    
    [self.sidePanelController showCenterPanelAnimated:YES];
    UINavigationController *vc = (UINavigationController *)self.sidePanelController.centerPanel;
    
    [vc pushViewController:registerController animated:YES];
    
}


-(void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UserLoginNoti" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UserLoginByMessageNoti"  object:nil];
}

@end
